home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Python 1.1 / Parser / intrcheck.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-28  |  4.3 KB  |  231 lines  |  [TEXT/KAHL]

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* Check for interrupts */
  26.  
  27. #ifdef THINK_C
  28. #include <MacHeaders>
  29. #define macintosh
  30. #endif
  31.  
  32. #ifdef HAVE_CONFIG_H
  33. #include "config.h"
  34. #endif
  35.  
  36. #include "myproto.h"
  37. #include "intrcheck.h"
  38.  
  39.  
  40. #ifdef QUICKWIN
  41.  
  42. #include <io.h>
  43.  
  44. void
  45. initintr()
  46. {
  47. }
  48.  
  49. int
  50. intrcheck()
  51. {
  52.     _wyield();
  53. }
  54.  
  55. #define OK
  56.  
  57. #endif /* QUICKWIN */
  58.  
  59. #ifdef _M_IX86
  60. #include <io.h>
  61. #endif
  62.  
  63. #if defined(MSDOS) && !defined(QUICKWIN)
  64.  
  65. #ifdef __GNUC__
  66.  
  67. /* This is for DJGPP's GO32 extender.  I don't know how to trap
  68.  * control-C  (There's no API for ctrl-C, and I don't want to mess with
  69.  * the interrupt vectors.)  However, this DOES catch control-break.
  70.  * --Amrit
  71.  */
  72.  
  73. #include <go32.h>
  74.  
  75. void
  76. initintr()
  77. {
  78.     _go32_want_ctrl_break(1 /* TRUE */);
  79. }
  80.  
  81. int
  82. intrcheck()
  83. {
  84.     return _go32_was_ctrl_break_hit();
  85. }
  86.  
  87. #else /* !__GNUC__ */
  88.  
  89. /* This might work for MS-DOS (untested though): */
  90.  
  91. void
  92. initintr()
  93. {
  94. }
  95.  
  96. int
  97. intrcheck()
  98. {
  99.     int interrupted = 0;
  100.     while (kbhit()) {
  101.         if (getch() == '\003')
  102.             interrupted = 1;
  103.     }
  104.     return interrupted;
  105. }
  106.  
  107. #endif /* __GNUC__ */
  108.  
  109. #define OK
  110.  
  111. #endif /* MSDOS && !QUICKWIN */
  112.  
  113.  
  114. #ifdef macintosh
  115.  
  116. #ifdef applec /* MPW */
  117. #include <OSEvents.h>
  118. #include <SysEqu.h>
  119. #endif /* applec */
  120.  
  121. #include <signal.h>
  122.  
  123. static int interrupted;
  124.  
  125. static RETSIGTYPE intcatcher PROTO((int));
  126. static RETSIGTYPE
  127. intcatcher(sig)
  128.     int sig;
  129. {
  130.     interrupted = 1;
  131.     signal(SIGINT, intcatcher);
  132. }
  133.  
  134. void
  135. initintr()
  136. {
  137.     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  138.         signal(SIGINT, intcatcher);
  139. }
  140.  
  141. int
  142. intrcheck()
  143. {
  144.     register EvQElPtr q;
  145.     
  146.     q = (EvQElPtr) GetEvQHdr()->qHead;
  147.     
  148.     for (; q; q = (EvQElPtr)q->qLink) {
  149.         if (q->evtQWhat == keyDown &&
  150.                 (char)q->evtQMessage == '.' &&
  151.                 (q->evtQModifiers & cmdKey) != 0) {
  152.             FlushEvents(keyDownMask, 0);
  153.             interrupted = 1;
  154.             break;
  155.         }
  156.     }
  157.     if (interrupted) {
  158.         interrupted = 0;
  159.         return 1;
  160.     }
  161.     return 0;
  162. }
  163.  
  164. #define OK
  165.  
  166. #endif /* macintosh */
  167.  
  168.  
  169. #ifndef OK
  170.  
  171. /* Default version -- for real operating systems and for Standard C */
  172.  
  173. #include <stdio.h>
  174. #include <string.h>
  175. #include <signal.h>
  176.  
  177. int interrupted;
  178.  
  179. /* ARGSUSED */
  180. static RETSIGTYPE
  181. #ifdef _M_IX86
  182. intcatcher(int sig)    /* So the C compiler shuts up */
  183. #else /* _M_IX86 */
  184. intcatcher(sig)
  185.     int sig; /* Not used by required by interface */
  186. #endif /* _M_IX86 */
  187. {
  188.     extern void goaway PROTO((int));
  189.     static char message[] =
  190. "python: to interrupt a truly hanging Python program, interrupt once more.\n";
  191.     switch (interrupted++) {
  192.     case 0:
  193.         break;
  194.     case 1:
  195.         write(2, message, strlen(message));
  196.         break;
  197.     case 2:
  198.         interrupted = 0;
  199.         goaway(1);
  200.         break;
  201.     }
  202.     signal(SIGINT, intcatcher);
  203. }
  204.  
  205. void
  206. initintr()
  207. {
  208.     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  209.         signal(SIGINT, intcatcher);
  210. #ifdef HAVE_SIGINTERRUPT
  211.     /* This is for SunOS and other modern BSD derivatives.
  212.        It means that system calls (like read()) are not restarted
  213.        after an interrupt.  This is necessary so interrupting a
  214.        read() or readline() call works as expected.
  215.        XXX On old BSD (pure 4.2 or older) you may have to do this
  216.        differently! */
  217.     siginterrupt(SIGINT, 1);
  218. #endif /* HAVE_SIGINTERRUPT */
  219. }
  220.  
  221. int
  222. intrcheck()
  223. {
  224.     if (!interrupted)
  225.         return 0;
  226.     interrupted = 0;
  227.     return 1;
  228. }
  229.  
  230. #endif /* !OK */
  231.